package com.bb.blog.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Component
public class MyAckReceiver implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //因为传递消息的时候用的map传递,所以将Map从Message内取出需要做些处理
            System.out.println("消费的主题消息来自：" + message.getMessageProperties().getConsumerQueue());
            channel.basicAck(deliveryTag, true);
//			channel.basicReject(deliveryTag, true);//为true会重新放回队列
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }

    }

//    private Map<String, String> mapStringToMap(String str,int entryNum ) {
//        str = str.substring(1, str.length() - 1);
//        String[] strs = str.split(",",entryNum);
//        Map<String, String> map = new HashMap<>(2);
//        for (String string : strs) {
//            String key = string.split("=")[0].trim();
//            String value = string.split("=")[1];
//            map.put(key, value);
//        }
//        return map;
//    }
}
